package component.filedialog;

import java.awt.Component;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class AskFileDialog {
	
	public static FileFilter matFilter = new FileFilter() {
		public String getDescription() { return "Matlab files (*.mat)"; }
		public boolean accept(File f) { return f.isDirectory() || f.getName().toUpperCase().endsWith(".MAT"); } 
	};

	public static FileFilter txtFilter = new FileFilter() {
		public String getDescription() { return "Text files (*.txt)"; }
		public boolean accept(File f) { return f.isDirectory() || f.getName().toUpperCase().endsWith(".TXT"); }
	};

	static FileFilter xlsFilter = new FileFilter() {
		public String getDescription() { return "Excel files (*.XLS)"; }
		public boolean accept(File f) { return f.isDirectory() || f.getName().toUpperCase().endsWith(".XLS"); } 
	};

	static FileFilter dirFilter = new FileFilter() {
		public String getDescription() { return "Directories"; }
		public boolean accept(File f) { return f.isDirectory(); } 
	};

	
	public static File askMatFile(File f, Component parent)
	{
		// init save dialog
		JFileChooser jfc = new JFileChooser(f);
		jfc.setFileFilter(matFilter);
		
		String fileName = f.getName();
		if (f.isFile() && fileName.indexOf('.') != -1) {
			fileName = fileName.substring(0, fileName.indexOf('.')) + ".mat";
		} 
		else if (f.isDirectory()) {
			fileName = f.getName() + ".mat";
		}
		else {
			fileName = "output.mat";
		}
		
		// show dialog
		jfc.setSelectedFile(new File(System.getProperty("user.home"), fileName));
		int r = jfc.showSaveDialog(parent);
		
		// if save pressed, save to file
		if (r == JFileChooser.APPROVE_OPTION) {
			return jfc.getSelectedFile();
		}
		return null;
	}


	public static File askExcelFile(File f, Component parent)
	{
		// init save dialog
		JFileChooser jfc = new JFileChooser(System.getProperty("user.home"));
		jfc.setFileFilter(xlsFilter);
		String fileName = "neuro_stats.xls";
		if (f != null) fileName = "stats_" + f.getName().replaceAll("\\..*", "").replaceAll("\\s", "_") + ".xls";
		
		// show dialog
		jfc.setSelectedFile(new File(System.getProperty("user.home"), fileName));
		int r = jfc.showSaveDialog(parent);
		
		// if save pressed, save to file
		if (r == JFileChooser.APPROVE_OPTION) {
			return jfc.getSelectedFile();
		}
		return null;
	}

	public static File askTxtFile(Component parent, boolean save)
	{
		// init dialog
		JFileChooser jfc = new JFileChooser();
		jfc.setFileFilter(txtFilter);

		// show dialog
		jfc.setSelectedFile(new File(System.getProperty("user.home"), "config.txt"));

		if (save) {
			int r = jfc.showSaveDialog(parent);

			// if save pressed, save to file
			if (r == JFileChooser.APPROVE_OPTION) {
				return jfc.getSelectedFile();
			}
			return null;
		}
		else {
			int r = jfc.showOpenDialog(parent);

			// if open pressed
			if (r == JFileChooser.APPROVE_OPTION) {
				return jfc.getSelectedFile();
			}
			return null;

		 }
	}
}
